/*
 * 软件版权: 恒生电子股份有限公司
 * 修改记录:
 * 修改日期     修改人员  修改说明
 * ========    =======  ============================================
 * 2021/4/16  lisy31662  新增
 * ========    =======  ============================================
 */
package com.sansi.pencilbook.controller;

import com.sansi.pencilbook.DO.ResponseDO;
import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.entity.Book;
import com.sansi.pencilbook.entity.RecommendBook;
import com.sansi.pencilbook.entity.User;
import com.sansi.pencilbook.service.BookServer;
import com.sansi.pencilbook.service.RecommendBookServer;
import com.sansi.pencilbook.service.UserBookRelationServer;
import com.sansi.pencilbook.service.UserServer;
import com.sansi.pencilbook.util.ResponseDOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 功能说明:
 *
 * @author lisy31662
 */
@Slf4j
@RestController
@RequestMapping("/api/recommendBook")
public class RecommendController extends VerifyController implements BaseController {

  @Autowired
  private RecommendBookServer recommendBookServer;

  @Autowired
  private BookServer bookServer;

  @Autowired
  private UserServer userServer;

  @GetMapping("/u/getRecommendBooks")
  public ResponseDO getRecommendBooks(HttpServletRequest request) {
    String userAccount = "";
    if (!"".equals(userAccount=getUserAccount(request))) {
      User user = (User) userServer.getUserByAccount(userAccount).getData();
      Result result = recommendBookServer.getRecommendBookByUser(user.getUserId());
      return getResponseDO(result);
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
    }
  }

  @GetMapping("/u/addRecommendBooks")
  public ResponseDO addRecommendBooks(HttpServletRequest request) {
    String userAccount = "";
    if (!"".equals(userAccount=getUserAccount(request))) {
      User user = (User) userServer.getUserByAccount(userAccount).getData();
      Set<Book> set = (Set<Book>) bookServer.getRecommendBooks(user.getUserId()).getData();
      List<RecommendBook> recommendBooks = new ArrayList<>();
      RecommendBook recommendBook;
      int i=0;
      for (Book book : set) {
        recommendBook = new RecommendBook(user.getUserId(), book.getBookIsbn(), book.getCategoryId(),book.getBookImg(),
            book.getBookName(), book.getBookPublishers(), book.getBookPrice(), book.getBookAuthor(), book.getBookIntroduction(), book.getBookTime(),i++);
        recommendBooks.add(recommendBook);
      }
      Result result = recommendBookServer.addRecommendBooks(recommendBooks);
      return result.isSuccess() ? ResponseDOUtil.setResponseDO(true, "添加推荐列表成功", recommendBooks) :
          ResponseDOUtil.setResponseDO(false, "添加推荐失败");
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
    }
  }

  @GetMapping("/u/updateRecommendBooks")
  public ResponseDO updateRecommendBooks(HttpServletRequest request) {
    String userAccount = "";
    if (!"".equals(userAccount=getUserAccount(request))) {
      User user = (User) userServer.getUserByAccount(userAccount).getData();
      Set<Book> set = (Set<Book>) bookServer.getRecommendBooks(user.getUserId()).getData();
      List<RecommendBook> recommendBooks = new ArrayList<>();
      List<RecommendBook> recommendBooksTmp = (List<RecommendBook>) recommendBookServer.getRecommendBookByUser(user.getUserId()).getData();
      RecommendBook recommendBook;
      int i=0;
      boolean flag = recommendBooksTmp!=null && set.size()==recommendBooksTmp.size();
      for (Book book : set) {
        recommendBook = new RecommendBook(user.getUserId(), book.getBookIsbn(), book.getCategoryId(),book.getBookImg(),
            book.getBookName(), book.getBookPublishers(), book.getBookPrice(), book.getBookAuthor(), book.getBookIntroduction(), book.getBookTime(),i);
        recommendBooks.add(recommendBook);
        flag = flag && Objects.equals(recommendBook, recommendBooksTmp.get(i));
        i++;
      }
      if (!flag) {
        Result result = recommendBookServer.updateUserRecommend(user.getUserId(), recommendBooks);
        return result.isSuccess() ? ResponseDOUtil.setResponseDO(true, "更新推荐列表成功", recommendBooks) :
            ResponseDOUtil.setResponseDO(false, "更新推荐失败");
      }else
        return ResponseDOUtil.setResponseDO(true, "更新推荐列表成功", recommendBooks);
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
    }
  }

  private ResponseDO getResponseDO(Result result)  {
    if (result.isSuccess()) {
      if (result.getEffectRow() > 0) {
        return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData());
      }else {
        return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg(), result.getData());
      }
    }else {
      return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
    }
  }

}
